package com.yj.springbootstudy.mulThread.demo;

/**
 * Object wait notify 案例
 */
public class WaitNotifyDemo {

    private static Object lock = new Object();
    private static boolean hasFood = false;
    private static boolean hasMoney = false;


    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            synchronized (lock) {


                while (!hasFood) {
                    System.out.println("没吃的干不了活！");
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                    System.out.println("吃的来的！可以干活了");

            }

        });


        Thread t2 = new Thread(() -> {
            synchronized (lock) {

                while (!hasMoney) {
                    System.out.println("没钱干不了活！");
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                    System.out.println("发钱了！可以干活了");

            }

        });
        t1.start();
        t2.start();
        Thread.sleep(3000);
        new Thread(()->{
           synchronized (lock){
               hasFood = true;
               lock.notify();
           }
        }).start();


    }


}
